package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.entity.Repair;
import com.qf.enums.AppHttpCodeEnum;
import com.qf.exception.SystemException;
import com.qf.mapper.RepairMapper;
import com.qf.result.ResponseResult;
import com.qf.service.HouseService;
import com.qf.service.OwnerService;
import com.qf.service.RepairService;
import com.qf.utils.BeanCopyUtils;
import com.qf.vo.RepairVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * (Repair)表服务实现类
 *
 * @author makejava
 * @since 2023-02-04 22:32:26
 */
@Service("repairService")
public class RepairServiceImpl extends ServiceImpl<RepairMapper, Repair> implements RepairService {

    @Autowired
    private HouseService houseService;

    @Autowired
    private OwnerService ownerService;


    @Override
    public ResponseResult getRepairList() {
        List<Repair> repairs = list();
        List<RepairVo> repairVos = BeanCopyUtils.copyBeanList(repairs, RepairVo.class);

        try {
            repairVos = repairVos.stream()
                    //过滤house_id为空的数据
                    .filter(repairVo -> repairVo.getHouseId() != null)
                    //过滤onwer_id为空的数据
                    .filter(repairVo -> ownerService.getById(houseService.getById(repairVo.getHouseId()).getOwnId()) != null)

                    .map(repairVo -> repairVo.setHouseNum(houseService.getById(repairVo.getHouseId()).getHouseNum()))
                    .map(repairVo -> repairVo.setOwnName(ownerService.getById(houseService.getById(repairVo.getHouseId()).getOwnId()).getOwnName()))
                    .collect(Collectors.toList());
        }catch (Exception e){
            return ResponseResult.errorResult(600,"其他模块错误,请和其他模块负责人联系");
        }
        return ResponseResult.okResult(repairVos);
    }

    @Override
    public ResponseResult deleteRepair(Integer id) {
        if (getById(id)==null){
            throw new SystemException(AppHttpCodeEnum.ID_ERROR);
        }
        removeById(id);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updateRepair(Repair repair) {
        if (getById(repair.getId())==null){
            throw new SystemException(AppHttpCodeEnum.ID_ERROR);
        }
        LambdaQueryWrapper<Repair> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Repair::getId,repair.getId());
        update(repair,queryWrapper);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult saveRepair(Repair repair) {
        if(repair.getId()!=null){
            return ResponseResult.errorResult(201,"id无需传值,因为这这是添加哦,下次记得别添加了");
        }
        save(repair);
        return ResponseResult.okResult();
    }


    @Override
    public ResponseResult likeRepairMsg(String repairMsg) {
        LambdaQueryWrapper<Repair> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Repair::getRepairMsg, repairMsg)
                .or()
                .like(Repair::getOwnPhone, repairMsg)
                .or()
                .like(Repair::getCreateTime,repairMsg)
                .or()
                .like(Repair::getUpdateTime,repairMsg);
        List<Repair> repairs = list(queryWrapper);
        List<RepairVo> repairVos = BeanCopyUtils.copyBeanList(repairs, RepairVo.class);
        repairVos = repairVos.stream()
                .map(repairVo -> repairVo.setHouseNum(houseService.getById(repairVo.getHouseId()).getHouseNum()))
                .map(repairVo -> repairVo.setOwnName(ownerService.getById(houseService.getById(repairVo.getHouseId()).getOwnId()).getOwnName()))
                .collect(Collectors.toList());

        return ResponseResult.okResult(repairVos);
    }

}
